    <div class="head"><h5 class="iList">Comments fields for template <?php echo $template->getName() ?></h5><div style="float:right;margin-right:10px; margin-top:5px;"><input type="button" id="go_back" value="<- Go back" class="greyishBtn submitForm" onclick="window.location.href='<?php echo  url_for("templates/index")?>'" ></div></div>
     <form action="<?php echo url_for('comment_fields/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight"><input type="text" name="comment_field[name]" value="<?php echo $form->getObject()->getName()?>"  class="validate[required]"  id="comment_field_name" style="width: 500px" /></div>
     		<div class="fix"></div>
     	</div>
     	
     	<div class="rowElem noborder">
     	<label for="name">Field Type:</label>
     		<div class="formRight">
     		
     			<select name="comment_field[field_type_id]" id="comment_field_field_type_id" style="min-width:240px;"  >
                   <?php 
                    foreach ($field_types as $field_type) {
                    	$selected = $form->getObject()->getFieldTypeId() == $field_type->getId() ? "selected" : "";
                    	?>
                    	<option <?php echo $selected ?>  value="<?php echo $field_type->getId(); ?>"><?php echo $field_type->getName(); ?></option>
                    	<?php 
                    }
                    ?>
                 </select>
     		
			</div>
     		<div class="fix"></div>
     	</div>
     	
     	<div class="rowElem noborder">
     		<span style="margin-left: 3px;position: relative;top: 4px;">Required:</span>
			<?php $checked = $form->getObject()->getRequired() == true ? "checked" : ""; ?>
			<input type="checkbox" id="comment_field_required" name="comment_field[required]" <?php echo $checked; ?> value="1" />
     		<div class="formRight">
			
			</div>
     		<div class="fix"></div>
     	</div>
     	
        <input type="hidden" name="comment_field[template_id]" id="comment_field_template_id" style="width:240px;" value="<?php echo $template->getId()?>"  />
     	
     	<div class="fix"></div>
        <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
     	<?php if (!$form->getObject()->isNew()){
     		?>
     		<input type="button" value="Create New" class="greyishBtn submitForm" id="create_new" />
     	<?php 
     	} ?>          
        <div class="fix"></div>
     </form>
     
     
<script type="text/javascript">

$("#save").click(function (event){
    submitForm();
})

$("#create_new").click(function (event){

    $.ajax({type: 'POST', url:' <?php echo url_for('comment_fields/index?template_id=' . $template->getId())?>', script:true, cache:false,
            success: function(data) {
            	$('#category_container_div').html(data);
             } });
             
})

$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('comment_fields/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
        if (!validate()){
			return false;
        }
         },
            success: function(data) {
                 $('#category_container_div').html(data);
             } });
}



		function validate(){
			var valid = true;
			var error_msg = "";
			if ($("#comment_field_name").attr("value") == ""){
				valid = false;
				error_msg = "Name required";
			};	
				
			if (!valid){
				$('#msg_text').html(error_msg);
				$('#error_msg').show('fast');
			}
			return valid;			
		}
		
		$('form').jqTransform({imgPath:'../images/forms'});
</script>     
